//
//  FRPGalleryViewController.m
//  FuncionalReactivePixels
//
//  Created by kiwik on 15-2-5.
//  Copyright (c) 2015年 Kiwik. All rights reserved.
//

#import "FRPGalleryViewController.h"
#import "FRPGalleryFlowLayout.h"
#import "FRPPhotoImporter.h"
#import "FRPCell.h"
#import "FRPFullSizePhotoViewController.h"
#import "RACDelegateProxy.h"

@interface FRPGalleryViewController ()

@property(nonatomic,strong) id collectionViewDelegate;

@end

@implementation FRPGalleryViewController

static NSString * const reuseIdentifier = @"Cell";

-(id)init
{
    FRPGalleryFlowLayout *flowLayout = [[FRPGalleryFlowLayout alloc] init];
    
    self = [self initWithCollectionViewLayout:flowLayout];
    if (!self) return nil;
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Configure self
    self.title = @"Popular on 500px";
    
    // Configure view
    [self.collectionView registerClass:[FRPCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    // Reactive Stuff
    @weakify(self);
    [RACObserve(self, photosArray) subscribeNext:^(id x) {
        @strongify(self);
        [self.collectionView reloadData];
    }];
    
    // Load data
//    [self loadPopularPhotos];
    
    RAC(self,photosArray)=[[[[FRPPhotoImporter importPhotos]
                             doCompleted:^{
                                 @strongify(self);
                                 [self.collectionView reloadData];
                             }] logError] catchTo:[RACSignal empty]];
    
    
//    RACDelegateProxy *viewControllerDelegate = [[RACDelegateProxy alloc]  initWithProtocol:@protocol(FRPFullSizePhotoViewControllerDelegate)];
//    [[viewControllerDelegate rac_signalForSelector:@selector(userDidScroll:toPhotoAtIndex:)
//                                      fromProtocol:@protocol(FRPFullSizePhotoViewControllerDelegate)]
//     subscribeNext:^(RACTuple *value) {
//         @strongify(self);
//         [self.collectionView scrollToItemAtIndexPath: [NSIndexPath indexPathForItem:[value.second integerValue] inSection:0]
//                                     atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:NO];
//     }];
//    
//    self.collectionViewDelegate = [[RACDelegateProxy alloc] initWithProtocol:@protocol(UICollectionViewDelegate)];
//    
//    [[self.collectionViewDelegate rac_signalForSelector:@selector(collectionView:didSelectItemAtIndexPath:)]
//     subscribeNext:^(RACTuple *arguments) {
//         NSLog(@"subscribeNext");
//         @strongify(self);
//         FRPFullSizePhotoViewController *viewController = [[FRPFullSizePhotoViewController alloc] initWithPhotoModels:self.photosArray
//                                                                                                    currentPhotoIndex:[(NSIndexPath *)arguments.second item]];
//         viewController.delegate = (id<FRPFullSizePhotoViewControllerDelegate>)viewControllerDelegate;
//         [self.navigationController pushViewController:viewController animated:YES];
//     }];
}

//-(void)loadPopularPhotos {
//    [[FRPPhotoImporter importPhotos] subscribeNext:^(id x) {
//        self.photosArray = x;
//    } error:^(NSError *error) {
//        NSLog(@"Couldn't fetch photos from 500px: %@", error);
//    }];
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)userDidScroll:(FRPFullSizePhotoViewController*)viewController toPhotoAtIndex:(NSInteger)index {
    [self.collectionView scrollToItemAtIndexPath:
     [NSIndexPath indexPathForItem:index inSection:0]
                                atScrollPosition:UICollectionViewScrollPositionCenteredVertically
                                        animated:NO];
}

#pragma mark <UICollectionViewDataSource>

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.photosArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    FRPCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier  forIndexPath:indexPath];
    [cell setPhotoModel:self.photosArray[indexPath.row]];
    return cell;
}

-(void)collectionView:(UICollectionView*)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    FRPFullSizePhotoViewController *viewController =[[FRPFullSizePhotoViewController alloc] initWithPhotoModels:self.photosArray
                                              currentPhotoIndex:indexPath.item];
    
    viewController.delegate = self;
    
    [self.navigationController pushViewController:viewController animated:YES];
}

#pragma mark <UICollectionViewDelegate>

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/

@end
